package com.zy.web.demo.duoxiancheng.demo8;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/7/29 14:21
 * 多线程对一个对象的实例进行操作的时候会出现非线程安全
 * 变量在方法内是安全的，还有给它锁
 */
public class Demo8 {

    private int num;

    synchronized public void addId(String name){
        try {
            if (name.equals("a")){
                num = 100;
                Thread.sleep(1000);
                System.out.println("a set over");
            }else {
                num = 200;
                System.out.println("b set over");
            }
            System.out.println(name+" num=" +num);

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

}
